﻿using AntdUI;
using BST250301;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace BST250301.Views
{
    public partial class Upgrad : UserControl
    {

        public Upgrad()
        {
            InitializeComponent();
            InitData();
            if (!MainWindow.userCurrent.IsSuperAdmin)
            { this.Enabled = false; }
        }
        private void InitData()
        {
            inputNumberRecordInterval.Value = (decimal)MainWindow.configapp.RecordInterval;
            inputNumberTCUInterval.Value = (decimal)MainWindow.configapp.TCUInterval;
            inputNumberShowListboxInterval.Value = (decimal)MainWindow.configapp.ShowListboxInterval;
            inputNumberFilter.Value = (decimal)MainWindow.configapp.FilterAlpha;
            inputNumberStable90.Value = (decimal)MainWindow.configapp.StableBar_90;
            inputNumberStable10.Value = (decimal)MainWindow.configapp.StableBar_10;
        }

        private void buttonSN_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(inputSN.Text.Trim()))
                return;
            var ret = DB.SetHashDate(inputSN.Text.Trim(), MainWindow.userCurrent.Account);
            if (ret > 0)
            {
                MessageBox.Show("请重新登陆");
                Program.LoginWindow.Show();
                Environment.Exit(0);
                //Application.Exit(); // 退出程序
            }
        }

        private void buttonUpgrad_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show($"是否确认更新？", "新版", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (!UpgradeSelf())
                    MessageBox.Show("启动更新失败，请联系管理员");
                //else
                //    MessageBox.Show("正在启动更新，请等候");
            }
        }
        private bool UpgradeSelf()
        {
            try
            {
                string serverip = MainWindow.configapp.AppAuthorMail;
                if (serverip.Contains("18605323323"))
                    serverip = "81.70.27.42";
                string serverport = "9000";
                if (!HslCommunication.Authorization.SetAuthorizationCode("8d367989-0ebb-43c1-b730-55c6af84784a"))
                {
                    MessageBox.Show("更新时AuthorizationCode错误，请联系管理员");
                    return false;
                }
                Process prs = System.Diagnostics.Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Upgrade.exe"), $"{serverip} {serverport} \"BST250301TestStand.exe\"");
                System.Threading.Thread.Sleep(10);
                Process.GetCurrentProcess().Kill();
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }

        private void buttonRDPara_Click(object sender, EventArgs e)
        {
            var rp = new RecordData();
            var form = new RecordPara(rp);// { Size = new Size(500, 350) };
            AntdUI.Modal.open(new AntdUI.Modal.Config("", form, TType.None)
            {
                BtnHeight = 0,
            });
            if (form.submit)
            {

            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                MainWindow.configapp.RecordInterval = (int)inputNumberRecordInterval.Value;
                MainWindow.configapp.TCUInterval = (int)inputNumberTCUInterval.Value;
                MainWindow.configapp.ShowListboxInterval = (int)inputNumberShowListboxInterval.Value;
                MainWindow.configapp.FilterAlpha = (float)inputNumberFilter.Value;
                MainWindow.configapp.StableBar_90 = (float)inputNumberStable90.Value;
                MainWindow.configapp.StableBar_10 = (float)inputNumberStable10.Value;
                MainWindow.configapp.Save();
                MessageBox.Show("已保存");
            }
            catch (Exception ex)
            {

            }

        }
        private void Upgrad_Load(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }
    }
}
